/**
 * Copyright 2010-present Facebook.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.widget;

import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;
import com.facebook.FacebookException;
import com.facebook.model.GraphUser;
import com.facebook.widget.FriendPickerFragment;
import com.facebook.widget.MyFragmentFriendPicker;
import com.facebook.widget.PickerFragment;
import com.twodstar.selfie.SelfieBattleApplication;
import com.twodstar.selfie.R;


/**
 * The PickerActivity enhances the Friend or Place Picker by adding a title
 * and a Done button. The selection results are saved in the ScrumptiousApplication
 * instance.
 */
public class ActivityPicker extends FragmentActivity 
{
    public static final Uri FRIEND_PICKER = Uri.parse("picker://friend");
    

    private FriendPickerFragment friendPickerFragment;  
  

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pickers);

        Bundle args = getIntent().getExtras();
        FragmentManager manager = getSupportFragmentManager();
        Fragment fragmentToShow = null;
        Uri intentUri = getIntent().getData();

        if (FRIEND_PICKER.equals(intentUri)) 
        {
            if (savedInstanceState == null) 
            {
                friendPickerFragment = new MyFragmentFriendPicker(args);
            } 
            else 
            {
                friendPickerFragment = (MyFragmentFriendPicker) manager.findFragmentById(R.id.picker_fragment);;
            }

            friendPickerFragment.setOnErrorListener(new PickerFragment.OnErrorListener() 
            {
                @Override
                public void onError(PickerFragment<?> fragment, FacebookException error) 
                {
                    ActivityPicker.this.onError(error);
                }
            });
            friendPickerFragment.setOnDoneButtonClickedListener(new PickerFragment.OnDoneButtonClickedListener() 
            {
                @Override
                public void onDoneButtonClicked(PickerFragment<?> fragment) 
                {
                    finishActivity();
                }
            });
            fragmentToShow = friendPickerFragment;

        } 
        else 
        {
            // Nothing to do, finish
            setResult(RESULT_CANCELED);
            finish();
            return;
        }

        manager.beginTransaction().replace(R.id.picker_fragment, fragmentToShow).commit();
    }

    @Override
    protected void onStart() 
    {
        super.onStart();
        if (FRIEND_PICKER.equals(getIntent().getData())) 
        {
            try 
            {
                friendPickerFragment.loadData(false);
                friendPickerFragment.setMultiSelect(false);
            } 
            catch (Exception ex) 
            {
                onError(ex);
            }
        } 
        
    }

    @Override
    protected void onStop() 
    {
        super.onStop();
       
    }

    private void onError(Exception error) 
    {
        String text = getString(R.string.check_internet);
        Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
        toast.show();
        
        
    }

   

    private void finishActivity() 
    {
        SelfieBattleApplication app = SelfieBattleApplication.Instance();
        if (FRIEND_PICKER.equals(getIntent().getData())) 
        {
            if (friendPickerFragment != null) 
            {
            	
            	List<GraphUser> Users = friendPickerFragment.getSelection();
            	if(Users != null && Users.size() > 0)
            		app.setSelectedUsers(Users);
            }
        } 
        
        setResult(RESULT_OK, null);
        finish();
    }
}
